package ch.rheintalconnection.sysventory.technicalServices.wmi;

import java.net.ConnectException;

import ch.rheintalconnection.sysventory.dto.LogicalDiskDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class LogicalDiskScan {

	private ActiveXComponent axWMI;
	private String host;

	public static final String QUERY_LOGICALDISK = "SELECT * FROM Win32_LogicalDisk";

	

	public LogicalDiskScan(ActiveXComponent axWMI, String host) {
		super();
		this.axWMI = axWMI;
		this.host = host;
	}



	public LogicalDiskDto scan() throws ConnectException {

		LogicalDiskDto logicalDiskDto = null;

		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY_LOGICALDISK));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;

		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
			
			String hdcapacity = Dispatch.call(item, "Size").toString();
			String freespace = Dispatch.call(item, "FreeSpace").toString();
			
			logicalDiskDto = new LogicalDiskDto(hdcapacity, freespace);
			return logicalDiskDto;
		}
		
		throw new ConnectException(host + " - Could not scan LogicalDisk");
	}
}
